<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/8/5
 * Time: 19:51
 */

namespace Api\Service;


class ScoreFavoriteService
{
    /**
     * 添加用户收藏谱单
     * @return mixed
     */
    public function addFavScore($param)
    {
        $model = M('ScoreFavorite');
        $map = array(
            'user_id' => $param['uid'],
            'score_id' => $param['score_id']
        );
        $res = $model->where($map)->select();
        if (!empty($res)) {
            //获取用户当前是否标记为喜欢状态
            if ($res[0]['is_delete'] == 1) {
                $data = array(
                    'is_delete'=>0,
                    'update_time'=>date( 'Y-m-d H:i:s', time())
                );
                $result = $model->where($map)->save($data);
                //谱单收藏数记录+1
                $scoreModel = M('Score');
                $res = $scoreModel->where(array('id'=>$param['score_id']))->field('favCount')->select();
                $score['favCount'] = (int)$res[0]['favCount'] + 1;
                $scoreModel->where(array('id'=>$param['score_id']))->save($score);
            } else {
                //取消收藏
                $data = array(
                    'is_delete'=>1,
                    'update_time'=>date( 'Y-m-d H:i:s', time() )
                );
                $result = $model->where($map)->save($data);
                //谱单收藏数记录-1
                $scoreModel = M('Score');
                $res = $scoreModel->where(array('id'=>$param['score_id']))->field('favCount')->select();
                $score['favCount'] = (int)$res[0]['favCount'] - 1;
                $scoreModel->where(array('id'=>$param['score_id']))->save($score);
            }
            return $result;
        } else {
            $data = $model->create($param);
            if ($data) {
                $param['user_id'] = $param['uid'];
                $param['create_time'] = date( 'Y-m-d H:i:s', time() );
                //添加谱单至收藏表
                $id = $model->add( $param );
                //谱单收藏数记录+1
                $scoreModel = M('Score');
                $res = $scoreModel->where(array('id'=>$param['score_id']))->field('favCount')->select();
                $score['favCount'] = (int)$res[0]['favCount'] + 1;
                $scoreModel->where(array('id'=>$param['score_id']))->save($score);
                return $id;
            } else {
                E($model->getError());
            }
        }
    }

    /**
     * 清除谱单收藏状态
     */
    public function delFavScore($cond)
    {
        $model = M('ScoreFavorite');
        $data = array(
            'is_delete' => 1
        );
        $count = $model->where($cond)->count();
        if (empty($count)) {
            return true;
        }
        $res = $model->where($cond)->save($data);
        return $res;
    }

    /**
     * 获取谱单收藏数
     */
    public function favCount($scoreId)
    {
        $map = array(
            'score_id'=>$scoreId,
            'is_delete' => 0
        );
        $count = M('ScoreFavorite')->where($map)->count();
        return $count;
    }
}